package com.sgcc.activiti.mapper;

import com.sgcc.activiti.domain.InstanceBusiness;
import com.sgcc.activiti.domain.TaskVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author 一只闲鹿
 */
@Repository
public interface TaskMapper {

    @Select(" <script>SELECT\n" +
            "\tA.*,ib.*\n" +
            "FROM\n" +
            "\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\tA.*\n" +
            "\t\tFROM\n" +
            "\t\t\t(\n" +
            "\t\t\t\tSELECT DISTINCT\n" +
            "\t\t\t\t\tRES.*\n" +
            "\t\t\t\tFROM\n" +
            "\t\t\t\t\tACT_RU_TASK RES\n" +
            "\t\t\t\tLEFT JOIN ACT_RU_IDENTITYLINK I ON I.TASK_ID_ = RES.ID_\n" +
            "\t\t\t\tWHERE\n" +
            "\t\t\t\t\tRES.SUSPENSION_STATE_ in (1,2)\n" +
            "\t\t\t\tAND (\n" +
            "\t\t\t\t\tRES.ASSIGNEE_ = #{userId}\n" +
            "\t\t\t\t\tOR (\n" +
            "\t\t\t\t\t\tRES.ASSIGNEE_ IS NULL\n" +
            "\t\t\t\t\t\tAND (\n" +
            "\t\t\t\t\t\t\tI.USER_ID_ = #{userId}\n" +
            "\t\t\t\t\t\t\tOR I.GROUP_ID_ IN (\n" +
            "\t\t\t\t\t\t\t\tSELECT\n" +
            "\t\t\t\t\t\t\t\t\tG.GROUP_ID_\n" +
            "\t\t\t\t\t\t\t\tFROM\n" +
            "\t\t\t\t\t\t\t\t\tACT_ID_MEMBERSHIP G\n" +
            "\t\t\t\t\t\t\t\tWHERE\n" +
            "\t\t\t\t\t\t\t\t\tG.USER_ID_ = #{userId}\n" +
            "\t\t\t\t\t\t\t)\n" +
            "\t\t\t\t\t\t)\n" +
            "\t\t\t\t\t)\n" +
            "\t\t\t\t)\n" +
            "\t\t\t\tORDER BY\n" +
            "\t\t\t\t\tRES.CREATE_TIME_ DESC\n" +
            "\t\t\t) A\n" +
            "\t) A\n" +
            "\tLEFT JOIN\n" +
            "\tinstance_business AS ib\n" +
            "\tON \n" +
            "\t\tib.instance_id = A.PROC_INST_ID_\n" +
            "WHERE\n" +
            "\t1 = 1 " +
            "<if test='module != null'> AND ib.module = #{module} </if>" +
            "<if test='lockState == 1 '> AND A.SUSPENSION_STATE_ = 1 </if>" +
            "<if test='lockState == 2 '> AND A.SUSPENSION_STATE_ = 2 and ib.update_by = #{userId}</if>" +
            "<if test='lockState == 3 '> AND A.SUSPENSION_STATE_ = 2 and ib.update_by &lt;&gt; #{userId}</if>" +
            "<if test='lockState == 4 '> AND A.SUSPENSION_STATE_ = 2  </if>" +
            "<if test='lockState == 5 '> AND (A.SUSPENSION_STATE_ = 1 or ib.update_by &lt;&gt; #{userId} )</if>" +
            "<if test='id != null'> AND ib.business_key = #{id} </if>" +
            "<if test='name != null'> AND A.NAME_ like #{name} </if>" +
            "<if test='startTime != null'> AND A.CREATE_TIME_ &gt;= #{startTime} </if>" +
            "<if test='endTime != null'> AND A.CREATE_TIME_ &lt;= #{endTime} </if>" +
            "<if test='taskId != null'> AND A.ID_ = #{taskId} </if> " +
            "<if test='procDefId != null'> AND A.PROC_DEF_ID_ like #{procDefId} </if> " +
            "<if test='taskName != null'> AND A.NAME_ = #{taskName} </if>" +
            "<if test='instanceId != null'> AND A.PROC_INST_ID_ = #{instanceId} </if> \n" +
            "LIMIT #{offset},\n" +
            " #{pageSize}</script> ")
    List<Map> findTodoList(TaskVo taskVo);

    @Select(" <script>SELECT\n" +
            "\tCOUNT(*) AS COUNT_\n" +
            "FROM\n" +
            "\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\tA.*\n" +
            "\t\tFROM\n" +
            "\t\t\t(\n" +
            "\t\t\t\tSELECT DISTINCT\n" +
            "\t\t\t\t\tRES.*\n" +
            "\t\t\t\tFROM\n" +
            "\t\t\t\t\tACT_RU_TASK RES\n" +
            "\t\t\t\tLEFT JOIN ACT_RU_IDENTITYLINK I ON I.TASK_ID_ = RES.ID_\n" +
            "\t\t\t\tWHERE\n" +
            "\t\t\t\t\tRES.SUSPENSION_STATE_ in (1,2)\n" +
            "\t\t\t\tAND (\n" +
            "\t\t\t\t\tRES.ASSIGNEE_ = #{userId}\n" +
            "\t\t\t\t\tOR (\n" +
            "\t\t\t\t\t\tRES.ASSIGNEE_ IS NULL\n" +
            "\t\t\t\t\t\tAND (\n" +
            "\t\t\t\t\t\t\tI.USER_ID_ = #{userId}\n" +
            "\t\t\t\t\t\t\tOR I.GROUP_ID_ IN (\n" +
            "\t\t\t\t\t\t\t\tSELECT\n" +
            "\t\t\t\t\t\t\t\t\tG.GROUP_ID_\n" +
            "\t\t\t\t\t\t\t\tFROM\n" +
            "\t\t\t\t\t\t\t\t\tACT_ID_MEMBERSHIP G\n" +
            "\t\t\t\t\t\t\t\tWHERE\n" +
            "\t\t\t\t\t\t\t\t\tG.USER_ID_ = #{userId}\n" +
            "\t\t\t\t\t\t\t)\n" +
            "\t\t\t\t\t\t)\n" +
            "\t\t\t\t\t)\n" +
            "\t\t\t\t)\n" +
            "\t\t\t\tORDER BY\n" +
            "\t\t\t\t\tRES.CREATE_TIME_ DESC\n" +
            "\t\t\t) A\n" +
            "\t) A\n" +
            "\tLEFT JOIN\n" +
            "\tinstance_business AS ib\n" +
            "\tON \n" +
            "\t\tib.instance_id = A.PROC_INST_ID_\n" +
            "WHERE\n" +
            "\t1 = 1 " +
            "<if test='module != null'> AND ib.module = #{module} </if>" +
            "<if test='lockState == 1 '> AND A.SUSPENSION_STATE_ = 1 </if>" +
            "<if test='lockState == 2 '> AND A.SUSPENSION_STATE_ = 2 and ib.update_by = #{userId}</if>" +
            "<if test='lockState == 3 '> AND A.SUSPENSION_STATE_ = 2 and ib.update_by &lt;&gt; #{userId}</if>" +
            "<if test='lockState == 4 '> AND A.SUSPENSION_STATE_ = 2  </if>" +
            "<if test='lockState == 5 '> AND (A.SUSPENSION_STATE_ = 1 or ib.update_by &lt;&gt; #{userId} )</if>" +
            "<if test='id != null'> AND ib.business_key = #{id} </if>" +
            "<if test='startTime != null'> AND A.CREATE_TIME_ &gt;= #{startTime} </if>" +
            "<if test='endTime != null'> AND A.CREATE_TIME_ &lt;= #{endTime} </if>" +
            "<if test='name != null'> AND A.NAME_ like #{name} </if>" +
            "<if test='taskId != null'> AND ID_ = #{taskId} </if>" +
            "<if test='procDefId != null'> AND PROC_DEF_ID_ like #{procDefId} </if>" +
            "<if test='taskName != null'> AND NAME_ = #{taskName} </if>" +
            "<if test='instanceId != null'> AND PROC_INST_ID_ = #{instanceId} </if> \n" +
            " </script> ")
    Integer findTodoCount(TaskVo taskVo);

    @Select(" <script>SELECT\n" +
            "\tA.*,ib.*\n" +
            "FROM\n" +
            "\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\tA.*\n" +
            "\t\tFROM\n" +
            "\t\t\t(\n" +
            "\t\t\t\tSELECT DISTINCT\n" +
            "\t\t\t\t\tRES.*\n" +
            "\t\t\t\tFROM\n" +
            "\t\t\t\t\tACT_HI_TASKINST RES\n" +
            "\t\t\t\tWHERE\n" +
            "\t\t\t\t\tRES.ASSIGNEE_ = #{userId}\n" +
            "\t\t\t\tAND RES.END_TIME_ IS NOT NULL\n" +
            "\t\t\t\tORDER BY\n" +
            "\t\t\t\t\tRES.END_TIME_ DESC\n" +
            "\t\t\t) A\n" +
            "\t\tWHERE\n" +
            "\t\t\t1 = 1\n" +
            "\t) A\n" +
            "\tLEFT JOIN\n" +
            "\tinstance_business AS ib\n" +
            "\tON \n" +
            "\t\tib.instance_id = A.PROC_INST_ID_\n" +
            "WHERE\n" +
            "\t1 = 1 " +
            "<if test='module != null'> AND ib.module = #{module} </if>" +
            "<if test='lockState == 1 '> AND A.SUSPENSION_STATE_ = 1 </if>" +
            "<if test='lockState == 2 '> AND A.SUSPENSION_STATE_ = 2 and ib.update_by = #{userId}</if>" +
            "<if test='lockState == 3 '> AND A.SUSPENSION_STATE_ = 2 and ib.update_by &lt;&gt; #{userId}</if>" +
            "<if test='lockState == 4 '> AND A.SUSPENSION_STATE_ = 2  </if>" +
            "<if test='lockState == 5 '> AND (A.SUSPENSION_STATE_ = 1 or ib.update_by &lt;&gt; #{userId} )</if>" +
            "<if test='startTime != null'> AND A.CREATE_TIME_ &gt;= #{startTime} </if>" +
            "<if test='endTime != null'> AND A.CREATE_TIME_ &lt;= #{endTime} </if>" +
            "<if test='id != null'> AND ib.business_key = #{id} </if>" +
            "<if test='name != null'> AND A.NAME_ like #{name} </if>" +
            "<if test='taskId != null'> AND ID_ = #{taskId} </if>" +
            "<if test='procDefId != null'> AND PROC_DEF_ID_ like #{procDefId} </if>" +
            "<if test='taskName != null'> AND NAME_ = #{taskName} </if>" +
            "<if test='instanceId != null'> AND PROC_INST_ID_ = #{instanceId} </if> \n" +
            "LIMIT #{offset},\n" +
            " #{pageSize}</script> ")
    List<Map> findDoneList(TaskVo taskVo);

    @Select(" <script>SELECT\n" +
            "\tCOUNT(*) AS COUNT_\n" +
            "FROM\n" +
            "\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\tA.*\n" +
            "\t\tFROM\n" +
            "\t\t\t(\n" +
            "\t\t\t\tSELECT DISTINCT\n" +
            "\t\t\t\t\tRES.*\n" +
            "\t\t\t\tFROM\n" +
            "\t\t\t\t\tACT_HI_TASKINST RES\n" +
            "\t\t\t\tWHERE\n" +
            "\t\t\t\t\tRES.ASSIGNEE_ =  #{userId}\n" +
            "\t\t\t\tAND RES.END_TIME_ IS NOT NULL\n" +
            "\t\t\t\tORDER BY\n" +
            "\t\t\t\t\tRES.END_TIME_ DESC\n" +
            "\t\t\t) A\n" +
            "\t\tWHERE\n" +
            "\t\t\t1 = 1\n" +
            "\t) A\n" +
            "\tLEFT JOIN\n" +
            "\tinstance_business AS ib\n" +
            "\tON \n" +
            "\t\tib.instance_id = A.PROC_INST_ID_\n" +
            "WHERE\n" +
            "\t1 = 1 " +
            "<if test='module != null'> AND ib.module = #{module} </if>" +
            "<if test='lockState == 1 '> AND A.SUSPENSION_STATE_ = 1 </if>" +
            "<if test='lockState == 2 '> AND A.SUSPENSION_STATE_ = 2 and ib.update_by = #{userId}</if>" +
            "<if test='lockState == 3 '> AND A.SUSPENSION_STATE_ = 2 and ib.update_by &lt;&gt; #{userId}</if>" +
            "<if test='lockState == 4 '> AND A.SUSPENSION_STATE_ = 2  </if>" +
            "<if test='lockState == 5 '> AND (A.SUSPENSION_STATE_ = 1 or ib.update_by &lt;&gt; #{userId} )</if>" +
            "<if test='id != null'> AND ib.business_key = #{id} </if>" +
            "<if test='startTime != null'> AND A.CREATE_TIME_ &gt;= #{startTime} </if>" +
            "<if test='endTime != null'> AND A.CREATE_TIME_ &lt;= #{endTime} </if>" +
            "<if test='name != null'> AND A.NAME_ like #{name} </if>" +
            "<if test='taskId != null'> AND ID_ = #{taskId} </if>" +
            "<if test='procDefId != null'> AND PROC_DEF_ID_ like #{procDefId} </if>" +
            "<if test='taskName != null'> AND NAME_ = #{taskName} </if>" +
            "<if test='instanceId != null'> AND PROC_INST_ID_ = #{instanceId} </if> \n" +
            " </script> ")
    Integer findDoneCount(TaskVo taskVo);

    @Insert(" INSERT INTO INSTANCE_BUSINESS (instance_id, business_key, module) VALUES (#{instanceId}, #{businessKey}, #{module}) ")
    int insertInstanceBusiness(InstanceBusiness instanceBusiness);

    @Select(" SELECT * FROM INSTANCE_BUSINESS WHERE 1=1 AND INSTANCE_ID = #{instanceId} LIMIT 1 ")
    Map<String, Object> selectInstanceBusinessByInstanceId(String instanceId);

    @Select(" SELECT * FROM ${module} WHERE ID = #{id} ")
    Map<String, Object> selectBusinessByBusinessKeyAndModule(@Param("id") String id, @Param("module") String module);

    @Select(" SELECT  `id`,  `instance_id`,  `business_key`,  `module`FROM `instance_business` WHERE `instance_id`=#{instanceId} ")
	Map<String, Object> findByInstanceId(@Param("instanceId") String instanceId);
    
    @Update("UPDATE  #{module}   SET   `STATUS` = '02', `FINISH_TIME` = now(),`HANDLE_NAME`=#{userId} ,`HANDLE_DESC`=#{handleDesc}   WHERE `id` = #{business_key}")
    void updataStatusAndFinshTime(@Param("business_key") String business_key,@Param("module")  Object module, @Param("userId") String userId, @Param("handleDesc") String handleDesc);

    @Update("UPDATE instance_business  SET   `update_by`=#{userName}, `update_time` = now() WHERE `instance_id` = #{instanceId}")
    void updataLockingUser(@Param("userName")String userName,@Param("instanceId")String instanceId);
}
